﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchemaExplorer;
using System.Data;

namespace SaturniLibrary.CodeGenerate
{
    public class TypeConverter
    {
        public static string GetCSharpType(ColumnSchema column)
        {
            string nullSymbol = column.AllowDBNull ? "?" : "";
            switch (column.DataType)
            {
                case DbType.AnsiString:
                    return "string";
                case DbType.Boolean:
                    return "bool" + nullSymbol;
                case DbType.Currency:
                case DbType.Decimal:
                case DbType.Double:
                    return "decimal" + nullSymbol;
                case DbType.Date:
                case DbType.Time:
                case DbType.DateTime:
                    return "DateTime" + nullSymbol;
                case DbType.Guid:
                    return "Guid";
                case DbType.Int16:
                    return "short" + nullSymbol;
                case DbType.Int32:
                    return "int" + nullSymbol;
                case DbType.Int64:
                    return "long" + nullSymbol;
                case DbType.Single:
                    return "float" + nullSymbol;
                case DbType.UInt16:
                case DbType.UInt32:
                case DbType.UInt64:
                    return "uint" + nullSymbol;
                default:
                    return "string";
            }
        }

        public static string GetSqlType(ColumnSchema column)
        {
            string param = column.NativeType;

            switch (column.DataType)
            {
                case DbType.Decimal:
                    {
                        param += "(" + column.Precision + ", " + column.Scale + ")";
                        break;
                    }
                case DbType.Int32:  //DbType.int
                case DbType.Int16:  //DbType.smallint
                case DbType.DateTime:  //DbType.DateTime
                case DbType.Byte:     //DbType.tinyint
                case DbType.Double:   //DbType.double/float
                    {
                        break;
                    }
                default:
                    {
                        if (column.Size == -1)
                        {
                            param += "(max)";
                        }
                        if (column.Size > 0)
                        {
                            param += "(" + column.Size + ")";
                        }
                        break;
                    }
            }
            // Debugger.Break(); not use bugger
            return param;
        }

        public static string GetCRTType(ColumnSchema column)
        {
            string nullSymbol = column.AllowDBNull ? "?" : "";
            switch (column.DataType)
            {
                case DbType.AnsiString:
                    return "String";
                case DbType.Boolean:
                    return "Boolean";
                case DbType.Currency:
                case DbType.Decimal:
                case DbType.Double:
                    return "Decimal";
                case DbType.Date:
                case DbType.Time:
                case DbType.DateTime:
                    return "DateTime";
                case DbType.Guid:
                    return "Guid";
                case DbType.Int16:
                    return "Int16";
                case DbType.Int32:
                    return "Int32";
                case DbType.Int64:
                    return "Int32";
                case DbType.Single:
                    return "float";
                case DbType.UInt16:
                case DbType.UInt32:
                case DbType.UInt64:
                    return "Uint32";
                default:
                    return "String";
            }
        }

    }
}
